/*
 * Copyright (c) 2013 Ohganic, Inc. All Rights Reserved.
 *
 *  CustomSharedPreferences.java
 *  Created on Jun 7, 2013
 */

package com.in.mobilerechargedth.common;

import java.util.Map;

import com.in.mobilerechargedth.constant.ConstantVariables;


import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * Custom Share Preferences. Include Set and Get
 *
 * <pre>
 * CustomSharedPreferences(Context)
 * Context is null will return by preferences type
 * 1. String : null
 * 2. Boolean: false
 * 3. Integer, Long, Float: 0
 * </pre>
 *
 */

public class CustomSharedPreferences {
    /** FEILD OF CLASS **/
    private static SharedPreferences sharedPre;
    private static SharedPreferences.Editor editor;
    private static Context appContext;


    /**
     * @Function name: init
     * @Description initiation values
     * @param context
     *            {@link Context}
     */
    public static void init(final Context context) {
        appContext = context;
        if (null != context) {
            sharedPre = context.getSharedPreferences(ConstantVariables.CUSTOM_SHAREDPREFERNCES, 0);

            editor = sharedPre.edit();
        } else {
            editor = null;
            sharedPre = null;
        }
    }

    /**
     * @Function name: refresh
     * @Description refresh
     */
    private static void refresh() {
        if (null != appContext) {
            sharedPre = appContext.getSharedPreferences(ConstantVariables.CUSTOM_SHAREDPREFERNCES, 0);
            editor = sharedPre.edit();

        } else {
            editor = null;
            sharedPre = null;
        }
    }

    /**
     * @Function name: setPreferences
     * @Description Set data for String
     *
     * @param preName
     *            Preferences name
     * @param value
     *            String input
     */
    public synchronized static void setPreferences(final String preName, final String value) {


        if (null != editor) {

            try {
                editor.putString(preName, value);
                editor.commit();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("Exception", e.getMessage());
            }
        }
    }

    /**
     * @Function name: getPreferences
     * @Description Get data for String
     *
     * @param preName
     *            Preferences name
     * @param defaultValue
     * @return String or 0 if Name not existed
     */
    public static String getPreferences(final String preName, final String defaultValue) {
        if (null != sharedPre) {
            return sharedPre.getString(preName, defaultValue);
        }

        return defaultValue;
    }

    /**
     * @Function name: setPreferences
     * @Description Set data for boolean
     *
     * @param preName
     *            Preferences name
     * @param value
     *            boolean input
     */
    public synchronized static void setPreferences(final String preName, final boolean value) {
        if (null != editor) {
            editor.putBoolean(preName, value);
            editor.commit();
        }
    }

    /**
     * @Function name: getPreferences
     * @Description Get data for boolean
     *
     * @param preName
     *            Preferences name
     * @param defaultValue
     * @return boolean or 0 if Name not existed
     */
    public static boolean getPreferences(final String preName, final boolean defaultValue) {
        if (null != sharedPre) {
            return sharedPre.getBoolean(preName, defaultValue);
        }

        return defaultValue;
    }

    /**
     * @Function name: setPreferences
     * @Description Set data for Integer
     *
     * @param preName
     *            Preferences name
     * @param value
     *            Integer input
     */
    public synchronized static void setPreferences(final String preName, final int value) {
        if (null != editor) {
            try {

                editor.putString(preName, preName);
                editor.commit();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.e("CustomSharedPreferences", e.getMessage());
            }
        }
    }

    /**
     * @Function name: getPreferences
     * @Description Get data for Integer
     *
     * @param preName
     *            Preferences name
     * @param defaultValue
     * @return Integer or 0 if Name not existed
     */
    public static int getPreferences(final String preName, final int defaultValue) {
        if (null != sharedPre) {

            return sharedPre.getInt(preName, defaultValue);
        }

        return defaultValue;
    }

    /**
     * @Function name: setPreferences
     * @Description Set data for Long
     *
     * @param preName
     *            Preferences name
     * @param value
     *            Long input
     */
    public synchronized static void setPreferences(final String preName, final long value) {
        if (null != editor) {
            try {
                editor.putLong(preName, value);
                editor.commit();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("Exception", e.getMessage());
            }
        }
    }

    /**
     * @Function name: getPreferences
     * @Description Get data for Long
     *
     * @param preName
     *            Preferences name
     * @param defaultValue
     * @return Long or 0 if Name not existed
     */
    public static long getPreferences(final String preName, final long defaultValue) {
        if (null != sharedPre) {

            try {
                return sharedPre.getLong(preName, defaultValue);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("Exception", e.getMessage());
            }
        }

        return defaultValue;
    }

    /**
     * @Function name: setPreferences
     * @Description Set data for Float
     *
     * @param preName
     *            Preferences name
     * @param value
     *            Float input
     */
    public synchronized static void setPreferences(final String preName, final float value) {
        if (null != editor) {
            try {
                editor.putFloat(preName, value);
                editor.commit();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("Exception", e.getMessage());
            }
        }
    }

    /**
     * @Function name: getPreferences
     * @Description Get data for Float
     *
     * @param preName
     *            Preferences name
     * @param defaultValue
     * @return Float or 0 if Name not existed
     */
    public static float getPreferences(final String preName, final float defaultValue) {
        if (null != sharedPre) {

            try {
                return sharedPre.getFloat(preName, defaultValue);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("Exception", e.getMessage());
            }
        }

        return defaultValue;
    }

    /**
     * @Function name: reset
     * @Description Reset sharedpreferences
     */
    public static void reset() {

        Map<String, ?> keys = sharedPre.getAll();

        for (Map.Entry<String, ?> entry : keys.entrySet()) {
            editor.remove(entry.getKey());
            editor.commit();
        }
    }

}
